<template>
  <el-card shadow="never" class="border-0">
    <!-- 搜索 -->
    <SearchForm :model="searchForm" @search="getList" @reset="resetSearchForm">
      <SearchItem label="">

        <div class="mt-4" style="width: 100%;margin-left: 0px !important;">

          <el-input v-model="searchForm.search" placeholder="消息名称" class="input-with-select">
            <template #prepend>
              <el-button :icon="Search" />
            </template>
            <template #append>
              <el-select v-model="searchForm.is_read" placeholder="请选择是否已读" style="width: 130px">
                <el-option key="0" label="全部" value="" />
                <el-option key="0" label="未读" value="0" />
                <el-option key="1" label="已读" value="1" />
              </el-select>
            </template>
          </el-input>
        </div>


      </SearchItem>
    </SearchForm>

    <!-- 新增|刷新 -->
    <ListHeader @create="handleCreate" @refresh="getList" />

    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="add_time" label="发布时间" :formatter="formatDate" width="180" />

      <el-table-column prop="title" align="center" label="消息标题" />
      <el-table-column prop="content" width="500" align="center" label="消息内容" />
      <el-table-column label="类型" width="200" align="center">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.type == 0">平台消息</el-tag>
          <el-tag type="warning" v-else>公司消息({{ scope.row.company_name }})</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template #default="scope">
          <el-button @click="handleEdit(scope.row)" text type="primary" class="p-1">
            <el-icon class="el-icon--left" :size="16">
              <Edit />
            </el-icon>修改
          </el-button>


          <el-popconfirm title="是否标记已读？" v-if="scope.row.is_read == '0'" confirmButtonText="确认" cancelButtonText="取消"
            @confirm="handleSetRead(scope.row.id)">
            <template #reference>
              <el-button text type="primary" class="p-1">
                <el-icon class="el-icon--left" :size="16">
                  <Position />
                </el-icon>未读
              </el-button>
            </template>
          </el-popconfirm>


          <el-popconfirm title="是否要删除该公告？" confirmButtonText="确认" cancelButtonText="取消"
            @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button text type="danger" class="p-1">
                <el-icon class="el-icon--left" :size="16">
                  <Delete />
                </el-icon>删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex items-center justify-center mt-5">
      <el-pagination background layout="prev, pager ,next" :total="total" :current-page="currentPage"
        :page-size="pageSize" @current-change="getList" />
    </div>

    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">

        <el-form-item label="发送对象" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio :label="1" aria-selected="true">全体员工</el-radio>
            <el-radio :label="2">部门员工</el-radio>
            <el-radio :label="3">部分员工</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item v-if="form.type == '2'" label="选择部门" prop="department_ids" class="mr-5">
          <el-select v-model="form.department_ids" filterable clearable multiple class="input-width" placeholder="部门">
            <el-option v-for="item in departmentList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.type == '3'" label="选择员工" prop="user_ids" class="mr-5">
          <el-select v-model="form.user_ids" filterable clearable multiple class="input-width" placeholder="员工">
            <el-option v-for="item in userAllList" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="消息标题" prop="title">
          <el-input v-model="form.title" placeholder="消息标题"></el-input>
        </el-form-item>

        <el-form-item label="消息内容" prop="content">
          <el-input v-model="form.content" placeholder="消息内容" type="textarea" :rows="5"></el-input>
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue";
import ListHeader from "~/components/ListHeader.vue";
import FormDrawer from "~/components/FormDrawer.vue";
import { useInitTable, useInitForm } from "~/composables/useCommon";
import SearchForm from "~/components/SearchForm.vue"
import SearchItem from "~/components/SearchItem.vue"
import { Search } from '@element-plus/icons-vue'

import {
  getUserMessageList,
  postUserMessage,
  putUserMessage, deleteUserMessage,
  setUserMessageRead
} from "~/api/message/index";
import {
  getALLUserList,
} from "~/api/user/index"

import { getALLDepartmentList } from "~/api/company/index"

import { useStore } from "vuex";
import { formatDateFun } from "~/composables/util";

const store = useStore();
const { formatDate } = formatDateFun();
const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  currentPage,
  total,
  pageSize,
  getList,
  handleDelete,
} = useInitTable({
  searchForm: { search: "", content: "", title: "" },
  requestListAPI: getUserMessageList,
  delete: deleteUserMessage,
  // pageSize: store.state.pageSize.MessageList,
  pageSize: 10,
  onGetListSuccess: (res) => {
    tableData.value = res.data.results;
    total.value = res.data.count;
  },
});
const {
  formDrawerRef,
  formRef,
  form,
  rules,
  editId,
  drawerTitle,
  handleSubmit,
  resetForm,
  handleCreate,
  handleEdit,
} = useInitForm({
  getList,
  create: postUserMessage,
  update: putUserMessage,
  form: {
    type: 1,
    title: "",
    content: "",
  },
  onGetDataSuccess: (res) => {
  },
  rules: {
    title: [
      {
        required: true,
        message: "消息标题不能为空",
        trigger: "blur",
      },
    ],
    content: [
      {
        required: true,
        message: "消息内容不能为空",
        trigger: "blur",
      },
    ],
  },
});

// 设置消息已读
const handleSetRead = (id) => {
  setUserMessageRead({ id: id }).then((res) => {
    getList()
  })
}



const userAllList = ref([])
const departmentList = ref([])
onMounted(() => {
  // 获取公司部门
  getALLDepartmentList().then((res) => {
    departmentList.value = res.data
  })

  // 获取用户信息
  getALLUserList({}).then((res) => {
    userAllList.value = res.data
  })
})
</script>